package com.library.library.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: 杜習田
 * @Date: 2019/8/4 16:57
 * @Description:
 */
@Component
public class RedisUtil {
    private static StringRedisTemplate stringRedisTemplate;
    private static RedisTemplate redisTemplate;

    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        RedisUtil.stringRedisTemplate = stringRedisTemplate;
    }
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate){
        RedisUtil.redisTemplate = redisTemplate;
    }

    public static Boolean setString(String key,String value) {
        stringRedisTemplate.opsForValue().set(key,value);
        return stringRedisTemplate.hasKey(key);
    }
    public static String getString(String key){
       return stringRedisTemplate.opsForValue().get(key);
    }
    public static Boolean setStringHours(String key,String value,Integer time) {
        stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.HOURS);
        return stringRedisTemplate.hasKey(key);
    }
    public static Boolean setStringMinutes(String key,String value,Integer time) {
        stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.MINUTES);
        return stringRedisTemplate.hasKey(key);
    }
    public static Boolean setStringSeconds(String key,String value,Integer time) {
        stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
        return stringRedisTemplate.hasKey(key);
    }
    public static Boolean setStringDays(String key,String value,Integer time) {
        stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.DAYS);
        return stringRedisTemplate.hasKey(key);
    }
    public static Boolean setObject(Object key,Object value){
         redisTemplate.opsForValue().set(key,value);
         return redisTemplate.hasKey(key);
    }
    public static Object getObject(Object key){
        return redisTemplate.opsForValue().get(key);
    }
    public static Boolean setObjectHours(String key,Object value,Integer time) {
        redisTemplate.opsForValue().set(key,value,time, TimeUnit.HOURS);
        return redisTemplate.hasKey(key);
    }
    public static Boolean setObjectMinutes(String key,Object value,Integer time) {
        redisTemplate.opsForValue().set(key,value,time, TimeUnit.MINUTES);
        return redisTemplate.hasKey(key);
    }
    public static Boolean setObjectSeconds(String key,Object value,Integer time) {
        redisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
        return redisTemplate.hasKey(key);
    }
    public static Boolean setObjectDays(String key,Object value,Integer time) {
        redisTemplate.opsForValue().set(key,value,time, TimeUnit.DAYS);
        return redisTemplate.hasKey(key);
    }
    public static Boolean hasString(String key){
        return stringRedisTemplate.hasKey(key);
    }
    public static Boolean hasObjest(String key){
        return redisTemplate.hasKey(key);
    }
}
